package cn.net.shuxing.yifu.common.front.controller;

import cn.net.shuxing.yifu.common.bean.ShareholderBuyGrantHistory;
import cn.net.shuxing.yifu.common.bean.ShareholderCoinGrantHistory;
import cn.net.shuxing.yifu.common.bean.User;
import cn.net.shuxing.yifu.common.constant.DealScene;
import cn.net.shuxing.yifu.common.constant.UserType;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.dto.BuyGrantReq;
import cn.net.shuxing.yifu.common.service.ShareholderBuyGrantHistoryService;
import cn.net.shuxing.yifu.common.service.ShareholderCoinGrantHistoryService;
import cn.net.shuxing.yifu.common.service.UserRoleService;
import cn.net.shuxing.yifu.common.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CoinGrantController {

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private UserService userService;

    @Autowired
    private ShareholderCoinGrantHistoryService shareholderCoinGrantHistoryService;

    @Autowired
    private ShareholderBuyGrantHistoryService shareholderBuyGrantHistoryService;

    /**
     * 获取我的配币记录
     *
     * @param userId
     * @param coinTypeId
     * @return
     */
    @RequestMapping("/front/coinGrant/info")
    public BaseDto getCoinGrantHistory(Integer userId, Integer coinTypeId) {
        User user = userService.getByPrimaryKey(userId);
        if (user == null)
            return BaseDto.error("此用户不存在");
        Integer type = user.getType();
        if (type != UserType.SHAREHOLDER)
            return BaseDto.error("请切换至股东身份");

        List<ShareholderCoinGrantHistory> shareholderCoinGrantHistoryList = shareholderCoinGrantHistoryService.getsByUserIdAndCoinType(userId, coinTypeId);

        return BaseDto.ok(shareholderCoinGrantHistoryList);
    }

    /**
     * 购买配币
     */
    @RequestMapping("/front/coinGrant/buy")
    public BaseDto buyGrantCoin(BuyGrantReq buyGrantReq) {
        Integer userId = buyGrantReq.getUserId();
        List<Integer> grantIds = buyGrantReq.getGrantIds();
        BigDecimal count = buyGrantReq.getCount();
        User user = userService.getByPrimaryKey(userId);
        if (user == null)
            return BaseDto.error("此用户不存在");
        Integer type = user.getType();
        if (type != UserType.SHAREHOLDER)
            return BaseDto.error("请切换至股东身份");
        Integer userState = userRoleService.getUserStateByUserIdAndUserType(userId, type);
        if (userState!=0)
            return BaseDto.error("账号状态异常");
        if (grantIds == null || grantIds.isEmpty())
            return BaseDto.error("请选择对应配币单");
        if (count == null)
            return BaseDto.error("请输入数量");

        return shareholderBuyGrantHistoryService.buy(userId, grantIds, count);
    }

    /**
     * 获取配发记录
     * @param userId
     * @param coinTypeId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/front/buyGrant/info")
    public BaseDto getBuyGrantHistory(Integer userId,
                                      Integer coinTypeId,
                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        List<ShareholderBuyGrantHistory> shareholderBuyGrantHistories = shareholderBuyGrantHistoryService.getByUserIdAndCoinType(userId, coinTypeId,pageNum,pageSize);
        return BaseDto.ok(shareholderBuyGrantHistories);
    }


}
